package com.chinasoft04.mall.controller;

import com.chinasoft04.mall.service.CategoryService;
import com.chinasoft04.mall.dto.Category;
import com.chinasoft04.mall.dto.Meta;
import com.chinasoft04.mall.dto.Rbody;
import lombok.AllArgsConstructor;
import org.springframework.lang.Nullable;
import org.springframework.web.bind.annotation.*;

@RestController
@CrossOrigin
@AllArgsConstructor
@RequestMapping("/api/private/v1")
public class CategoryController {

    CategoryService categoryService;

    @GetMapping("/categories")
    public Rbody getCategories(Integer type, @Nullable Integer pagenum, @Nullable Integer pagesize){
        Rbody rbody = new Rbody();
        Object categories = categoryService.getCategories(type, pagenum, pagesize);
        if (categories == null){
            Meta meta = new Meta();
            meta.setStatus(500);
            meta.setMsg("获取失败");
            rbody.setMeta(meta);
        }else {
            rbody.getMeta().setMsg("获取成功");
            rbody.setData(categories);
        }
        return rbody;
    }

    @PostMapping("/categories")
    public Rbody insertCategory(@RequestBody Category category){
        System.out.println("cont  "+category);
        Rbody rbody = new Rbody();
        Category newCategory = categoryService.insertCategory(category);
        if (newCategory == null){
            Meta meta = new Meta();
            meta.setStatus(500);
            meta.setMsg("创建失败");
            rbody.setMeta(meta);
        }else {
            rbody.getMeta().setMsg("创建成功");
            rbody.setData(newCategory);
            rbody.getMeta().setStatus(201);
        }
        return rbody;
    }

    @GetMapping("/categories/{id}")
    public Rbody getOneCategory(@PathVariable Integer id){
        Rbody rbody = new Rbody();
        if (id == null){
            rbody.getMeta().setStatus(400);
            rbody.getMeta().setMsg("参数不能为空");
        }else {
            Category category = categoryService.getOneCategory(id);
            if (category == null){
                Meta meta = new Meta();
                meta.setStatus(500);
                meta.setMsg("获取失败");
                rbody.setMeta(meta);
            }else {
                rbody.getMeta().setMsg("获取成功");
                rbody.setData(category);
            }
        }
        return rbody;
    }

    @PutMapping("/categories/{id}")
    public Rbody updateCategory(@PathVariable Integer id, @RequestBody Category category1){
        Rbody rbody = new Rbody();
        if (id != null && !(category1.getCat_name().isEmpty())){
            Category category = categoryService.updateCategory(id, category1.getCat_name());
            if (category == null){
                Meta meta = new Meta();
                meta.setStatus(500);
                meta.setMsg("更新失败");
                rbody.setMeta(meta);
            }else {
                rbody.getMeta().setMsg("更新成功");
                rbody.setData(category);
            }
        }else {
            rbody.getMeta().setStatus(400);
            rbody.getMeta().setMsg("参数不能为空");
        }
        return rbody;
    }

    @DeleteMapping("/categories/{id}")
    public Rbody deleteCategory(@PathVariable Integer id){
        Rbody rbody = new Rbody();
        if (id == null){
            rbody.getMeta().setStatus(400);
            rbody.getMeta().setMsg("参数不能为空");
        }else {
            int flag = categoryService.deleteCategory(id);
            if (flag != 1){
                Meta meta = new Meta();
                meta.setStatus(500);
                meta.setMsg("删除失败");
                rbody.setMeta(meta);
            }else {
                rbody.getMeta().setMsg("删除成功");
            }
        }
        return rbody;
    }
}
